package com.learn.algorithm.dynamic.programming;

public class LeetCode467 {
    public static int findSubstringInWraproundString(String str) {
        int n = str.length();
        int[] s = new int[n];
        for (int i = 0; i < n; i++) {
            s[i] = (str.charAt(i) - 'a');
        }
        int[] dp = new int[26];
        dp[s[0]] = 1;
        for (int i = 1, len = 1; i < n; i++) {
            int pre = s[i - 1];
            int cur = s[i];
            if ((pre == 25 && cur == 0) || (pre + 1 == cur)) {
                len++;
            } else {
                len = 1;
            }
            dp[cur] = Math.max(len, dp[cur]);
        }
        int ans = 0;
        for (int i = 0; i < 26; i++) {
            ans += dp[i];
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(findSubstringInWraproundString("a"));
        System.out.println(findSubstringInWraproundString("cac"));
        System.out.println(findSubstringInWraproundString("zab"));
    }
}
